//
//  YTHomeMainController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/9/10.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTHomeMainController.h"
#import "YTHomeBannerCell.h"
#import "YTHomeArticleCell.h"
#import "YTHomeMarqueeCell.h"
#import "YTHomeBanner.h"
#import "YTNewFlashModel.h"
#import "YTHomeDetailController.h"

NSString *khomeBannerCellID = @"khomeBannerCellID";
NSString *khomeArticleCellID = @"khomeArticleCellID";
NSString *khomeMerCellID = @"khomeMerCellID";

@interface YTHomeMainController ()<UITableViewDataSource ,UITableViewDelegate>
@property(nonatomic ,strong)UITableView *tableView;
@property(nonatomic ,strong)YTHomeBanner *banner;
@property(nonatomic ,strong)NSArray *flashList;
@property(nonatomic ,strong)YTHomeMainModel *model;

@property(nonatomic ,assign)int page;
@end

@implementation YTHomeMainController

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;

        if (self.isHot) {
            // 注册可重用cell
            [_tableView registerClass:[YTHomeBannerCell class]
               forCellReuseIdentifier:khomeBannerCellID];
            
            [_tableView registerNib:[UINib nibWithNibName:@"YTHomeMarqueeCell" bundle:nil]
             forCellReuseIdentifier:khomeMerCellID];
        }
        
        [_tableView registerNib:[UINib nibWithNibName:@"YTHomeArticleCell" bundle:nil]
         forCellReuseIdentifier:khomeArticleCellID];
    
        
        // setupRefreshHeader RefreshFooter
        YTRefreshHeader *header = [YTRefreshHeader headerWithRefreshingBlock:^{

            [self loadDataWithPullup:false];
        }];
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            [self loadDataWithPullup:YES];
        }];


        [footer setTitle:@"没有更多了哦～" forState:MJRefreshStateNoMoreData];
        [footer setTitle:@"" forState:MJRefreshStateIdle];


        _tableView.backgroundColor = [UIColor colorWithHex:@"f1f2f2"];


        _tableView.mj_header = header;
        _tableView.mj_footer = footer;
        
        //_tableView.contentInset = UIEdgeInsetsZero;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.page = 1;

    
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    
    [self.view addSubview:self.tableView];
//    [self loadDataWithPullup:false];
    [self.tableView.mj_header beginRefreshing];
}


- (void)loadDataWithPullup:(BOOL)pullup
{

    if (self.isHot) {
        
        if (!pullup) {
            [YTNetworkManager.shareManager requestBannerWithCompletion:^(YTHomeBanner *banner, BOOL isSuccess, NSString *error)
             {
                 if (!isSuccess) {
                     // 加载失败
                     return ;
                 }
                 [YTNetworkManager.shareManager requestNewFlashWithCompletion:^(NSArray *list, BOOL isSuccess, NSString *error)
                  {
                      // 请求失败
                      if (!isSuccess) {
                          return ;
                      }
                      
                      self.flashList = list;
                      self.banner = banner;
                      
                      [self.tableView reloadData];
                  }];
             }];
        }
    }
    
    if (pullup) {
        
        if (self.model.list.count == self.model.total.intValue) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
    }else{
        self.tableView.mj_footer.state = MJRefreshStateIdle;
    }
    
    [YTNetworkManager.shareManager requestHomeListWithTerm_id:self.term_id
                                                        paged:self.page
                                                    page_size:10
                                                   menu_order:!pullup
                                                      mintime:pullup ? self.model.list.lastObject.post_date : nil
                                                   completion:^(YTHomeMainModel *model, BOOL isSuccess, NSString *error)
     {
         if (!isSuccess) {
             [self.view showErrorWithTipStr:error];
             pullup ? [self.tableView.mj_footer endRefreshing] : [self.tableView.mj_header endRefreshing];

             return ;
         }
         
         // 下拉刷新
         if (!pullup) {
             self.model = model;
             [self.tableView.mj_header endRefreshing];
         }else{
             
             self.model.list = [self.model.list arrayByAddingObjectsFromArray:model.list];
             [self.tableView.mj_footer endRefreshing];
         }
         
         [self.tableView reloadData];
         
     }];
   
}


#pragma mark: - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.isHot ? self.model.list.count + 2 : self.model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID;
    
    if (self.isHot) {
        switch (indexPath.row)
        {
            case 0:
                cellID = khomeBannerCellID;
                break;
            case 1:
                cellID = khomeMerCellID;
                break;
            default:
                cellID = khomeArticleCellID;
                break;
        }
    }else{
        cellID = khomeArticleCellID;
    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
        
        if (cellID == khomeBannerCellID) {
            [(YTHomeBannerCell *)cell setBanner:self.banner];
            [(YTHomeBannerCell *)cell setBannerClickBlock:^(NSString *ID)
             {
                 YTHomeDetailController *detailVC = [[YTHomeDetailController alloc] init];
                 detailVC.ID = ID;
                 [self.navigationController pushViewController:detailVC animated:true];
             }];
        }
        
        
        
        if (cellID == khomeMerCellID) {
            [(YTHomeMarqueeCell *)cell setFlashList:self.flashList];
            [(YTHomeMarqueeCell *)cell setMarqueeClickBlock:^(YTNewFlashModel *model)
             {
                 YTDetailNewController *vc = [YTDetailNewController controller];
                 vc.ID = model.ID;
                 [self.navigationController pushViewController:vc animated:true];
             }];
        }
        
        if (cellID == khomeArticleCellID) {
            [(YTHomeArticleCell *)cell setArticle:self.isHot ? self.model.list[indexPath.row - 2] : self.model.list[indexPath.row]];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
   
    
    return cell;
}

#pragma mark: - table view delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.isHot ? ((indexPath.row == 0) ? (iPhone5 ? 150 : 200) : (indexPath.row == 1) ? (iPhone5 ? 70 : 80) : (iPhone5 ? 80 : 104)) : (iPhone5 ? 80 : 104);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row <= 1 && self.isHot) {
        return;
    }
    YTHomeArticle *article = self.isHot ? self.model.list[indexPath.row - 2] : self.model.list[indexPath.row];
    
    YTHomeDetailController *detailVC = [[YTHomeDetailController alloc] init];
    detailVC.ID = article.ID;
    [self.navigationController pushViewController:detailVC animated:true];
}

@end
